#!/usr/bin/env bash
# Copyright (c) 2020 The Toltec Contributors
# SPDX-License-Identifier: MIT

pkgnames=(folly)
pkgdesc="Z-machine interpreter for interactive fiction"
url="https://github.com/bkirwi/folly"
pkgver=0.0.1-4
timestamp=2022-04-18T17:50:16Z
section=games
maintainer="Ben Kirwin <ben@kirw.in>"
license=MIT
installdepends=(display)
makedepends=(build:libclang-14-dev build:clang-14 build:llvm-14-dev)

image=rust:v2.3

# Whitespace-separated list of source archives that are needed to build the package
source=(
    https://github.com/bkirwi/folly/archive/fe47fedbc9b77207de89c34c8133fa0d66e5079b.zip
    folly.draft
)
# SHA-256 checksums of the source archives above
sha256sums=(
    16d572cbe78605baa7df66e148e2a5083efd5a9642e2a033bf626207a1568a87
    SKIP
)

build() {
    local cpp_includes
    local gcc_version
    gcc_version="$("$CROSS_COMPILE"gcc -dumpfullversion)"
    cpp_includes="$(realpath "$SYSROOT"/../include/c++/"$gcc_version")"

    # Arguments for bindgen's usage of clang
    export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=$SYSROOT -I$cpp_includes \
        -I$cpp_includes/$NGCONFIG"

    # Arguments for the gcc-based tensorflow build in the build.rs file of tflite-rs.
    export TFLITE_RS_MAKE_TARGET_TOOLCHAIN_PREFIX="$CROSS_COMPILE"
    export TFLITE_RS_MAKE_EXTRA_CFLAGS="$BINDGEN_EXTRA_CLANG_ARGS \
        -mfpu=neon -mfloat-abi=hard -mcpu=cortex-a9"

    cargo build --release

    # Save the build system the trouble of stripping unused binaries
    rm -r target/release/build
    rm -r target/*/release/build
}

package() {
    # Install the app binary and the draft file
    install -D -m 755 "$srcdir"/target/*/release/folly "$pkgdir"/opt/bin/folly
    install -D -m 644 "$srcdir"/folly.draft "$pkgdir"/opt/etc/draft/folly.draft
}
